package com.strategy.core;

import lombok.Getter;

import java.util.List;
import java.util.Map;

/**
 * 组合策略，支持多个策略的链式执行
 */
@Getter
public class CompositeStrategy<T, P, R> extends AbstractStrategy<T, P, R> {

    // 前置策略列表
    private final List<BaseStrategy<?, P, ?>> preStrategies;

    // 最终执行的策略
    private final BaseStrategy<?, P, R> finalStrategy;

    public CompositeStrategy(String key, String version, String description,
                             List<BaseStrategy<?, P, ?>> preStrategies,
                             BaseStrategy<?, P, R> finalStrategy) {
        setKey(key);
        setVersion(version);
        setDescription(description);
        this.preStrategies = preStrategies;
        this.finalStrategy = finalStrategy;
    }

    @Override
    @SuppressWarnings("unchecked")
    public R execute(T input, Map<String, P> params) {
        Object result = input;

        // 执行前置策略链
        if (preStrategies != null && !preStrategies.isEmpty()) {
            for (BaseStrategy<?, P, ?> strategy : preStrategies) {
                result = ((BaseStrategy<Object, P, Object>) strategy).execute(result, params);
            }
        }

        // 执行最终策略
        return ((BaseStrategy<Object, P, R>) finalStrategy).execute(result, params);
    }

}